package cn.com.longfuinfo.wx.controller;


import java.util.List;

import com.jfinal.aop.Duang;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import cn.com.longfuinfo.wx.common.SysConfig;
import cn.com.longfuinfo.wx.dao.Goods;
import cn.com.longfuinfo.wx.dao.Store;
import cn.com.longfuinfo.wx.dao.User;
import cn.com.longfuinfo.wx.dao.UserLook;
import cn.com.longfuinfo.wx.dao.WxUser;
import cn.com.longfuinfo.wx.service.GoodsService;
import cn.com.longfuinfo.wx.service.UserService;

public class GoodsController extends BaseController{
	private GoodsService service = new GoodsService();
	private UserService userservice = Duang.duang(UserService.class);
	
	/**
	 * 商品列表
	 */
	public void index(){
		//一级分类代码
		String codeStr = getPara("code");
		if(!StrKit.isBlank(codeStr)){
			setAttr("code", codeStr);
		}
		render("index.jsp");
	}
	
	/**
	 * 异步查询商品分页列表
	 * @param pageNum 页码
	 * @param pageSize 数量
	 */
/*	public void page(){
		int pageNum = getParaToInt("pageNum",1);
		int pageSize = getParaToInt("pageSize",PropKit.getInt("constants.pageSize"));
		renderSuccess(service.findByPage(pageNum,pageSize));
	}*/
	/**
	 * 商品分类
	 */
	public void fenlei(){
		WxUser user=getCurrentWxUser();
		if(user==null){
			redirect("/index");
			return;
		}
		if(getSessionAttr("cityId")!=null){
			int cityId=getSessionAttr("cityId");
			setAttr("cityId", cityId);
			setAttr("title", service.findBabyTitle());
			render("sort.jsp");
		}else{
			redirect("/index");
		}
	}
	public void chanqian(){
		WxUser user=getCurrentWxUser();
		if(user==null){
			redirect("/index");
			return;
		}
		if(getSessionAttr("cityId")!=null){
			int cityId=getSessionAttr("cityId");
			setAttr("cityId", cityId);
			render("chanqian.jsp");
		}else{
			redirect("/index");
		}
		
	}
	public void chanhou(){
		WxUser user=getCurrentWxUser();
		if(user==null){
			redirect("/index");
			return;
		}
		if(getSessionAttr("cityId")!=null){
			int cityId=getSessionAttr("cityId");
			setAttr("cityId", cityId);
			render("chanhou.jsp");
		}else{
			redirect("/index");
		}
	}
	/**
	 * 活动集中营(活动列表)
	 */
	public void doingList(){
		setAttr("doingList", service.getHodingList());
		render("doingList.jsp");
	}
	/**
	 * 辣妈说
	 */
	public void momsay(){
		
		render("okmomsay.jsp");
	}
	/**
	 * 妈咪优选
	 */
	public void youxuan(){
		WxUser user=getCurrentWxUser();
		if(user==null){
			redirect("/index");
			return;
		}
	/*	setAttr("shiyong", service.fingShiYong());
		setAttr("zhuanti", service.fingzhuanti());
		setAttr("shiyongbaogao", service.fingShiYongBaoGao());*/
		render("mamiyouxuan.jsp");
	}
	
	public void mamiList(){
		int pageNum = getParaToInt("pageNum",1);
		int pageSize = getParaToInt("pageSize",PropKit.getInt("constants.pageSize"));
		renderSuccess(service.mamiList(pageNum,pageSize));
	}
	/**
	 * 积分列表
	 * @param paytype1：全部，2：商品积分，3：教育积分，4：健康积分
	 * @param kind：默认1 :销量优先排序，2：兑换积分升序，3：兑换积分降序
	 * @param type：默认1 :货币，2：积分，3：积分+货币
	 */
	public void list(){
		User users= userservice.findByUserid(getCurrentWxUserid());		
			int paytype = getParaToInt("paytype",1);			
			int kind = getParaToInt("kind",1);//排序方式			
			int type = getParaToInt("type",1);
			String minPrice=getPara("minPrice");//价格
			String maxPrice=getPara("maxPrice");
			String minIntegral=getPara("minIntegral");//积分
			String maxIntegral=getPara("maxIntegral");
			setAttr("paytype", paytype);
			setAttr("kind", kind);
			setAttr("type", type);
			setAttr("minPrice", minPrice);
			setAttr("maxPrice", maxPrice);			
			setAttr("minIntegral", minIntegral);
			setAttr("maxIntegral", maxIntegral);
			
			//判断用户是否注册
			if(users==null){
				redirect("/regist");
				setAttr("sppoint",0);
				setAttr("jkpoint",0);
				setAttr("jypoint",0);
			}else{
				setAttr("sppoint",users.getInt("sppoint"));
				setAttr("jkpoint", users.getInt("jkpoint"));
				setAttr("jypoint", users.getInt("jypoint"));
			}
			//商品类型 
			if(type==1){
				render("HSClist.jsp");
			}else if(type==2){
				render("list.jsp");
			}else{
				render("jfZKlist.jsp");
			}	
	}

	/**
	 *  
	 * 商品列表+排序
	 * @param paytype 1：全部，2：商品积分，3：教育积分，4：健康积分
	 * @param kind 1：销量优先排序，2：兑换积分升序，3：兑换积分降序
	 * @param type：默认1 :货币，2：积分，3：积分+货币
	 */
	public void jfPage(){
		int cityId=getSessionAttr("cityId");
		int pageNum = getParaToInt("pageNum",1);
		int pageSize = getParaToInt("pageSize",PropKit.getInt("constants.pageSize"));
		int kind = getParaToInt("kind",1);
		int paytype = getParaToInt("paytype",1);
		int type = getParaToInt("type",1);
		String minPrice=getPara("minPrice");
		String maxPrice=getPara("maxPrice");
		
		String minIntegral=getPara("minIntegral");
		String maxIntegral=getPara("maxIntegral");
		
		Page<Goods> page = service.findByJFPage(cityId,kind,paytype,type,minPrice,maxPrice,minIntegral,maxIntegral,pageNum,pageSize);
		System.out.println(page);
		renderSuccess(page);
		return;
	
	}

	
	/**
	 * 商品详情
	 */
	public void jfdetail(){
		int goodsid = getParaToInt(0, 0);
		String storeid = getPara("storeid");
		int type=getParaToInt("type",0);
		setAttr("type",type);
		if(goodsid == 0){
			redirect("/index");
    		return;
		}else{
			//查询商品详情信息
			Goods goods;
			if(getCurrentWxUser()!=null){
				//增加商品日浏览量
				Goods gos=service.getGoodById(goodsid);
				Record goodSeller = service.getGoodsSellerById(goodsid);
				String sellerId = goodSeller.get("sellerId");//商家端分配的id
				if(!StrKit.notNull(gos)){
					redirect("/index");
		    		return;
				}
				gos.set("daylook", gos.getInt("daylook")+1).update();
				//如果当前有用户登录，则查询商品详情的同时，查询该用户与该商品的收藏关系
				goods = service.findById(getCurrentWxUserid(),goodsid);
				//统计商品浏览量
				UserLook look=service.findLook(goodsid);
				if(StrKit.notNull(look)){
					look.set("conNum", look.getInt("conNum")+1).update();
				}else{
					UserLook userlook=new UserLook();
					userlook.set("goodsid", goodsid).set("conNum", 1).save();
				}
				if(StrKit.notNull(goods)){
					try {
						//ok商家（SE233814893）十月商家（SE201028362）所属店铺 产后商品现金购买不赠送积分，异业购买所有商品都不赠送积分，
						double sppoint=0;
						double jkpoint=0;
						double jypoint=0;
						if("SE233814893".equals(sellerId)||"SE201028362".equals(sellerId)){
							//产品分类：713：产前，714：产后
							if(goods.getInt("ftype")!=714){
								sppoint = SysConfig.getSppoint();
								jkpoint = SysConfig.getJkpoint();
								jypoint = SysConfig.getJypoint();
							}
						}
						setAttr("sppoint", sppoint);
						setAttr("jkpoint", jkpoint);
						setAttr("jypoint", jypoint);
					} catch (Exception e) {
						e.printStackTrace();
					}
					setAttr("adList", service.findImage(goods.get("LunBoNum")));
					String gooditem=goods.getStr("goodItem");
					String good_store=goods.getStr("good_store");
					if(!StrKit.isBlank(gooditem)){
						List<Record> goodItem=service.getGoodsItem(gooditem);
						if(goodItem.size()>0){
							setAttr("goodItem", goodItem);
						}
					}
					if(!StrKit.isBlank(good_store)){
						List<Record> store=service.getStore(good_store);
						if(store.size()>0){
							setAttr("store", store);
							if(StrKit.isBlank(storeid)){
								setAttr("store1", store.get(0));
								setAttr("active","1");
							}else{
								setAttr("store1", service.getStoreById(storeid));
								setAttr("active","2");
							}
						}
					}else{
						logger.info("active0==============>");
					}
				}
			}else{
				redirect("/index");
	    		return;
			}
			
			Record specs=service.getGoodsPaySpec(goodsid,type);
			if(specs==null){
				System.out.println("specs===null");
				specs=service.getGoodsSpecType(goodsid);//只有一种支付方式
			}
			
			setAttr("goods", goods);
			setAttr("goodsid", goodsid);
			
			setAttr("specsList",service.getGoodsSpec(goodsid));//根据id全部查询
			setAttr("specs",specs);	
			setAttr("userid",getSessionAttr("userid"));
			render("jfdetail.jsp");
		}
	}

	/**
	 * @param 根据规格id查询支付方式
	 * @param pageNum 页码
	 * @param pageSize 数量
	 */
	public void pageBySpec(){		
        int specId=getParaToInt("specId",0);
        if(specId == 0){
			redirect("/index");
    		return;
		}else{
		renderSuccess(service.getGoodsSpecId(specId));
		}
	}
	/**
	 * @param 根据商品ID和支付方式查询
	 * @param pageNum 页码
	 * @param pageSize 数量
	 */
	public void pageByPaySpec(){		
        int goodsId=getParaToInt("goodsId",0);
        int payId=getParaToInt("payId",0);
        int specId=getParaToInt("specId",0);
        if(goodsId == 0 || payId==0){
			redirect("/index");
    		return;
		}else{
		renderSuccess(service.getGoodsPaySpecId(goodsId,payId,specId));
		}
	}
	public void getGoods(){
		String goodsid = getPara("goodsid");
		if(StrKit.isBlank(goodsid)){
			redirect("/index");
			return;
		}
		Goods goods= service.findGoodsById(goodsid);
		if(StrKit.notNull(goods)){
			renderSuccess();return;
		}else{
			renderFailed();return;
		}
	}
	/**
	 * 商品详情
	 */
	public void detail(){
		int goodsid = getParaToInt("goods", 0);
		String storeid=getPara("storeid");
		if(goodsid == 0){
			redirect("/index");
    		return;
		}else{
			//查询商品详情信息
			Goods goods;
			if(getCurrentWxUser()!=null){
				//如果当前有用户登录，则查询商品详情的同时，查询该用户与该商品的收藏关系
				goods = service.findById(getCurrentWxUserid(),goodsid);
				if(StrKit.notNull(goods)){
					setAttr("adList", service.findDetailImage(goods.get("LunBoNum")));
					String guige=goods.getStr("guige");
					if(StrKit.notBlank(guige)){
						String[]list=guige.split("/");
						setAttr("list", list);
					}
				}
				setAttr("goods", goods);
				setAttr("storeid", storeid);
			}else{
				redirect("/index");
	    		return;
			}
		}
		render("details.jsp");
		return;
	}
	
	/**
	 * @param 首页积分超实惠 
	 * @param pageNum 页码
	 * @param pageSize 数量
	 * @param cityId 城市
	 */
	public void pageByjf(){
		WxUser user=getCurrentWxUser();
		if(user==null){
			redirect("/index");
			return;
		}
		int pageNum = getParaToInt("pageNum",1);
		int pageSize = getParaToInt("pageSize",3);
		int cityId = getParaToInt("cityId",0);
		renderSuccess(service.pageByjf(cityId, pageNum, pageSize));
	}
	
	/**
	 * @param 首页猜你喜欢
	 * @param pageNum 页码
	 * @param pageSize 数量
	 * @param cityId 城市
	 */
//	public void pageByxy(){
//		WxUser user=getCurrentWxUser();	
//		//测试用
//		//WxUser user=WxUser.me().findById(1024);
//		if(user==null){
//			redirect("/index");
//			return;
//		}else{
//			int pageNum = getParaToInt("pageNum",1);
//			int pageSize = getParaToInt("pageSize",50);
//			int cityId = getParaToInt("cityId",0);
//			int userId=getCurrentWxUser().getInt("id");
//			//int userId=user.getInt("id");
//			Record order=service.findLastOrder(userId);
//			int goods=0;
//			if(order !=null){
//				goods=order.getInt("goodsid");
//				Goods good = service.getGoodById(goods);
//				if(StrKit.notNull(good)){
//					renderSuccess(service.pageByxys(cityId, pageNum, pageSize,goods));
//				}else{
//					renderSuccess(service.pageByxy(cityId, pageNum, pageSize));
//				}
//			}else{
//				renderSuccess(service.pageByxy(cityId, pageNum, pageSize));
//			}
//		}
//	}
	/**
	 * @param 首页展示商品（畅销热品） 
	 * @param pageNum 页码
	 * @param pageSize 数量
	 * @param cityId 城市
	 */
	public void pageBesell(){
		WxUser user=getCurrentWxUser();
		//测试用
		//WxUser user=WxUser.me().findById(1024);
		if(user==null){
			redirect("/index");
			return;
		}else{
			int pageNum = getParaToInt("pageNum",1);
			int pageSize = getParaToInt("pageSize",50);
			int cityId = getParaToInt("cityId",0);
			renderSuccess(service.pageBesell(pageNum, pageSize, cityId));	
		}
	}
	/**
	 * @param 分类店长推荐
	 * @param pageNum 页码
	 * @param pageSize 数量
	 */
	public void pageBytui(){
		WxUser user=getCurrentWxUser();
		if(user==null){
			redirect("/index");
			return;
		}else{
			int pageNum = getParaToInt("pageNum",1);
			int pageSize = getParaToInt("pageSize",3);
			renderSuccess(service.pageBytui( pageNum, pageSize));
		}
	}
	/**
	 * @param 分类最新上市
	 * @param pageNum 页码
	 * @param pageSize 数量
	 * @param cityId 城市
	 */
	public void pageByxin(){
		WxUser user=getCurrentWxUser();
		if(user==null){
			redirect("/index");
			return;
		}else{
			int pageNum = getParaToInt("pageNum",1);
			int pageSize = getParaToInt("pageSize",50);
			int cityId = getParaToInt("cityId",0);
				renderSuccess(service.pageByxin(cityId, pageNum, pageSize));
		}
	}
	/**
	 * @param 产前猜你喜欢
	 * @param pageNum 页码
	 * @param pageSize 数量
	 * @param cityId 城市
	 */
	public void pageBycq(){
		WxUser user=getCurrentWxUser();
		if(user==null){
			redirect("/index");
			return;
		}else{
			int pageNum = getParaToInt("pageNum",1);
			int pageSize = getParaToInt("pageSize",50);
			int cityId = getParaToInt("cityId",0);
			renderSuccess(service.pageBycq(cityId, pageNum, pageSize));
		}
	}
	/**
	 * @param 产后猜你喜欢
	 * @param pageNum 页码
	 * @param pageSize 数量
	 * @param cityId 城市
	 */
	public void pageBych(){
		WxUser user=getCurrentWxUser();
		if(user==null){
			redirect("/index");
			return;
		}else{
			int pageNum = getParaToInt("pageNum",1);
			int pageSize = getParaToInt("pageSize",50);
			int cityId = getParaToInt("cityId",0);
			renderSuccess(service.pageBych(cityId, pageNum, pageSize));
		}
	}
	/**
	 * @param 列表
	 * @param pageNum 页码
	 * @param pageSize 数量
	 * @param cityId 城市
	 */
	public void pageGoodsList(){
		WxUser user=getCurrentWxUser();
		if(user==null){
			redirect("/index");
			return;
		}else{
			if(getSessionAttr("cityId")!=null){
				int categoryonid=getParaToInt("categoryonid",0);
				int categorytwid=getParaToInt("categorytwid",0);
				Record record=service.getCateName(categorytwid);
				setAttr("categoryName", record.get("categoryName"));
				setAttr("categorytwid", categorytwid);
				setAttr("categoryonid", categoryonid);
				int cityId=getSessionAttr("cityId");
				setAttr("cityId", cityId);
				render("goodslist.jsp");
			}else{
				redirect("/index");
			}
		}
	}
	/**
	 * @param 商品列表
	 * @param pageNum 页码
	 * @param pageSize 数量
	 * @param cityId 城市
	 */
	public void goodList(){
		WxUser user=getCurrentWxUser();
		if(user==null){
			redirect("/index");
			return;
		}else{
			int pageNum = getParaToInt("pageNum",1);
			int pageSize = getParaToInt("pageSize",10);
			int cityId = getParaToInt("cityId",0);
			int categoryonid=getParaToInt("categoryonid",0);
			int categorytwid=getParaToInt("categorytwid",0);
			renderSuccess(service.goodList(cityId, pageNum, pageSize,categoryonid,categorytwid));
		}
	}
	
	/**
	 * 关键字搜索
	 */
	public void serchkey(){
		String keyword = getPara("keyword");
		if(getSessionAttr("cityId")!=null){
			setAttr("keyword", keyword);
			render("search.jsp");
		}else{
			redirect("/index");
		}
	
	}
	public void serchList(){
		String keyword = getPara("keyword");
		int pageNum = getParaToInt("pageNum",1);
		int pageSize = getParaToInt("pageSize",50);
		if(getSessionAttr("cityId")!=null){
			int cityId=getSessionAttr("cityId");
			renderSuccess(service.serchList(cityId, pageNum, pageSize,keyword));
		}else{
			redirect("/index");
		}
	
	}
	/**
	 * 店铺详情
	 */
	public void shopInfo(){
		String storeid = getPara(0);
		if(StrKit.isBlank(storeid)){
			redirect("/index");
    		return;
		}else{
			//查询店铺详情信息
			Store store;
			if(getCurrentWxUser()!=null){
				 store=service.findStore(storeid);
				 if(store !=null){
				    String sellerId=store.getStr("sellerId");
					List<Record> list=service.storeList( storeid,sellerId);
					setAttr("size", list.size());
					setAttr("adList", service.findStoreImage(store.getStr("stroeimagenum")));
					setAttr("store", store);
					setAttr("aKtive", 2);//从店铺详情页在进入商品页,立即兑换 在确认订单页面显示商家地址
					render("shop.jsp");
				 }else{
					 redirect("/index");
			    		return;
				 }
				 
			}else{
				redirect("/index");
	    		return;
			}
			
		}
	}
	/**
	 * @param 店铺商品列表
	 * @param pageNum 页码
	 * @param pageSize 数量
	 * @param cityId 城市
	 */
	public void pageStore(){
		WxUser user=getCurrentWxUser();
		String storeid = getPara("storeid");
		if(user==null){
			redirect("/index");
			return;
		}else{
			int pageNum = getParaToInt("pageNum",1);
			int pageSize = getParaToInt("pageSize",50);
			renderSuccess(service.pageStore( pageNum, pageSize,storeid));
		}
	}
	/**
	 * @param 店铺列表
	 * @param pageNum 页码
	 * @param pageSize 数量
	 * @param cityId 城市
	 */
	public void storeList(){
		WxUser user=getCurrentWxUser();
		String storeid = getPara("storeid");
		String sellerId = getPara("sellerId");
		if(user==null){
			redirect("/index");
			return;
		}else{
			List<Record> list=service.storeList( storeid,sellerId);
			setAttr("stores", service.getStoreById(storeid));
			setAttr("storeList", list);
			setAttr("type", 1);
			render("storeList.jsp");
		}
	}
	/**
	 * @param 店铺列表
	 * @param pageNum 页码
	 * @param pageSize 数量
	 * @param cityId 城市
	 */
	public void goodsOtherStore(){
		WxUser user=getCurrentWxUser();
		String storeid = getPara("storeid");
		String sellerId = getPara("sellerId");
		int goodsid=getParaToInt("goodsid",0);
		if(user==null){
			redirect("/index");
			return;
		}else{
			List<Record> list=service.goodsOtherStore(storeid,sellerId,goodsid);
			setAttr("stores", service.getStoreById(storeid));
			setAttr("goodsid", goodsid);
			setAttr("storeList", list);
			setAttr("type", 2);
			render("storeList.jsp");
		}
	}
}
